<template>
  <div class="container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="用户账号" prop="mobile">
        <el-input
          style="width: 200px"
          maxlength="16"
          v-model.trim="queryParams.mobile"
          placeholder="请输入用户账号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在城市" prop="cityName">
        <el-select v-model="queryParams.cityName" placeholder="请选择">
          <el-option v-for="item in cityNameOptions" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" size="default" :data="dataList">
      <el-table-column label="用户账号" min-width="150px" align="center" prop="mobile" />
      <el-table-column
        label="用户名称"
        min-width="150px"
        align="center"
        show-overflow-tooltip
        prop="nickName"
      />
      <el-table-column label="联系方式" min-width="150px" align="center" prop="mobile" />
      <el-table-column label="所在城市" align="center" prop="cityName" />
      <el-table-column label="当前积分" align="center" prop="integral">
        <template #default="{ row }">
          <el-link :underline="false" type="primary" @click="handleRecords(row)">
            {{ row.integral }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="已使用" width="130px" align="center" prop="consumeIntegral" />
      <el-table-column label="最近交易时间" width="155px" align="center" prop="latelyTradingHour" />
    </el-table>
    <div style="margin-right: 10px">
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <RecordsDialog ref="recordsRef" />
  </div>
</template>

<script setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import { deleteProductApi, getPointListApi, updateProductApi } from '@/api/shopping'
import RecordsDialog from './cpts/recordsDialog.vue'
import useGlobalStore from '@/store/modules/global'

const globalStore = useGlobalStore()
const cityNameOptions = computed(() => globalStore.myCityList)

const queryRef = ref(null)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  mobile: '',
  cityName: ''
})
const total = ref(0)
const dataList = ref([])
const loading = ref(false)

// 当前积分记录
const recordsRef = ref(null)
function handleRecords(row) {
  recordsRef.value.init(row.clientId)
}

// ======================= 搜索 ======================
function getList() {
  loading.value = false
  getPointListApi(queryParams)
    .then((res) => {
      loading.value = false
      dataList.value = res?.rows || []
      total.value = res?.total || 0
    })
    .catch(() => {
      loading.value = false
      dataList.value = []
      total.value = 0
    })
}
// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

getList()
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
}
</style>
